package parser.convertToXml;

import java.io.File;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

/**
 * Permet de convertir un fichier au format iTunes en un fichier au format XML
 * exploitable par l'application.
 * 
 * @author buthane
 */
public class XsltITunes
{
	public void convert(String xmlFilename, String xsltFileName,
			String xmlResultFilename) throws TransformerException
	{
		// System.out.println("transformation in progress...");
		File xmlFile = new File(xmlFilename);
		File xsltFile = new File(xsltFileName);
		File xmlResultFile = new File(xmlResultFilename);

		Source xmlSource = new StreamSource(xmlFile);
		Source xsltSource = new StreamSource(xsltFile);
		Result result = new StreamResult(xmlResultFile);

		// create an instance of TransformerFactory
		TransformerFactory transFact = TransformerFactory.newInstance();

		Transformer trans = transFact.newTransformer(xsltSource);

		trans.transform(xmlSource, result);
		// System.out.println("transformation done");
	}

	public static void main(String[] args)
	{
		XsltITunes xslt = new XsltITunes();
		try
		{
			// xslt.convert("./ressource/iTunes Music Library.xml",
			// "./ressource/Precompute iTunes.xslt",
			// "./ressource/PreSmart iTunes Music Library.xml");

			xslt.convert("./ressource/PreSmart iTunes Music Library.xml",
					"./ressource/iTunes.xslt",
					"./ressource/Smart iTunes Music Library.xml");
		}
		catch (TransformerException e1)
		{
			e1.printStackTrace();
		}
	}
}
